%% Author: sq
%% Created: 2010-6-5
%% Description: TODO: Add description to my
-module(my).

%%
%% Include files
%%

%%
%% Exported Functions
%%
-export([]).

%%
%% API Functions
%%
-export([start/0, say/2,start2/0,pong/0,ping/2,ping/1,start3/0]).
say(What, 0) ->
    done;
say(What, Times) ->
    io:format("this is a good  |~w~n", [What]),
    say(What, Times - 1).
start() ->
    spawn(my, say, [hello, 3]),
    spawn(my, say, [goodbye, 3]).



ping(0, Pong_PID) ->
    Pong_PID ! finished,
    io:format("ping finished~n", []);
ping(N, Pong_PID) ->
    Pong_PID ! {ping, self()},
    receive
        pong ->
            io:format("Ping received pong~n", [])
    end,
    ping(N - 1, Pong_PID).
pong() ->
    receive
        finished ->
            io:format("Pong finished~n", []);
        {ping, Ping_PID} ->
            io:format("Pong received ping~n", []),
            Ping_PID ! pong,
            pong()
    end.
start2() ->
    Pong_PID = spawn(my, pong, []),
    spawn(my, ping, [3, Pong_PID]).

ping(0) ->
    pong ! finished,
    io:format("ping finished~n", []);
ping(N) ->
    pong ! {ping, self()},
    receive
        pong ->
            io:format("Ping received pong~n", [])
    end,
    ping(N - 1).


start3() ->
    register(pong, spawn(my, pong, [])),
    spawn(my, ping, [3]).
%%
%% Local Functions
%%

